def heapify(arr, n, i):
    # 将以i为根节点的子树调整为大根堆
    largest = i
    left = 2 * i + 1
    right = 2 * i + 2
    if left < n and arr[left] > arr[largest]:
        largest = left
    if right < n and arr[right] > arr[largest]:
        largest = right
    if largest != i:
        arr[largest], arr[i] = arr[i], arr[largest]
        heapify(arr, n, largest)

def heap_sort(arr):
    n = len(arr)
    # 构建大根堆
    for i in range(n // 2, -1, -1):
        heapify(arr, n, i)
    # 逐个取出堆顶元素，放到末尾，重新构建堆
    for i in range(n - 1, 0, -1):
        arr[0], arr[i] = arr[i], arr[0]
        heapify(arr, i, 0)

# 测试集 
#arr = [2,6,8,9,1,4];    
#heap_sort(arr)
#print(arr)
arr = []
num_elements = int(input("请输入你想要输入的数组元素的数量: "))  
for i in range(num_elements):
    element = int(input(f"请输入第{i+1}个元素: "))
    arr.append(element)
print("排序之后的数组为：")    
heap_sort(arr)
print(arr)
print("最大值为：")
print(arr[num_elements-1])
print("最小值为：")
print(arr[0])